package com.imay.live.logic;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import com.imay.base.util.StringUtil;
import com.imay.live.activity.MergeNoticeListActivity;
import com.imay.live.activity.ProfileActivity;
import com.imay.live.activity.RoomLiveShortActivity;
import com.imay.live.fragment.NoticeChildFragment;
import com.imay.live.model.NoticeMergeMineFollowModel;
import com.imay.live.model.NoticeMergeMineModel;
import com.imay.live.model.NoticeMineFollowModel;
import com.imay.live.model.NoticeModel;
import com.imay.live.model.UserModel;
import com.imay.base.util.SpannableStringUtils;
import com.imay.live.utils.TimeUtil;
import com.imay.live.R;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by  nick on 2017/4/18 0018.
 * 通知tab 的列表显示逻辑
 */

public class NoticeListLogic {

    static class MyClickableSpan extends ClickableSpan {

        private Context context;
        private long id;
        private boolean isMerge;
        private int noticeType;
        private int eventType = -1;

        public MyClickableSpan(Context context, long id, boolean isMerge, int noticeType) {
            this.context = context;
            this.id = id;
            this.isMerge = isMerge;
            this.noticeType = noticeType;
            this.eventType = -1;
        }

        public MyClickableSpan(Context context, long id, boolean isMerge, int noticeType, int eventType) {
            this.context = context;
            this.id = id;
            this.isMerge = isMerge;
            this.noticeType = noticeType;
            this.eventType = eventType;
        }

        @Override
        public void onClick(View widget) {
            if (eventType == 9) {
                RoomLiveShortActivity.startLiveActivity(context, id, true);
            } else {
                if (isMerge) {
                    MergeNoticeListActivity.start(context, noticeType, id);
                } else {
                    ProfileActivity.start(context, id);
                }
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {//这里是改变字的样式的，如果不添加会显示系统默认的绿色加下划线的样式
            super.updateDrawState(ds);
            ds.setColor(context.getResources().getColor(R.color.text_20));
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.setUnderlineText(false);
        }
    }

    static class SupperLiveClickableSpan extends ClickableSpan {

        private Context context;
        private long id;
        private int eventType = -1;


        public SupperLiveClickableSpan(Context context, long id, int eventType) {
            this.context = context;
            this.id = id;
            this.eventType = eventType;
        }

        @Override
        public void onClick(View widget) {
            if (eventType == 9) {
                RoomLiveShortActivity.startLiveActivity(context, id, true);
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {//这里是改变字的样式的，如果不添加会显示系统默认的绿色加下划线的样式
            super.updateDrawState(ds);
            ds.setColor(context.getResources().getColor(R.color.text_4400));
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.setUnderlineText(false);
        }
    }

    private static void addUserNameSpanable(Context context, SpannableStringUtils.Builder builder, UserModel model, int noticeType) {
        if (model != null) {
            builder.append(model.getNick()).setClickSpan(new MyClickableSpan(context, model.getUid(), false, noticeType));
        }
    }

    /**
     * 超胆播跳转
     *
     * @param context
     * @param builder
     * @param info
     * @param noticeType
     * @param eventType
     */
    public static void addSuperLiveSpanable(Context context, SpannableStringUtils.Builder builder, NoticeModel.NoticeShortLiveInfo info, int noticeType, int eventType) {
        if (info != null) {
            builder.append("#" + info.getChallengeTitle()).setClickSpan(new SupperLiveClickableSpan(context, info.getShortLiveId(), eventType));
        }
    }

    private static void addAtUserNameSpanable(Context context, SpannableStringUtils.Builder builder, UserModel model, int noticeType) {
        builder.append("@" + model.getNick()).setClickSpan(new MyClickableSpan(context, model.getUid(), false, noticeType));
    }

    private static void addNormalText(Context context, SpannableStringUtils.Builder builder, String text) {
        builder.append(text).setForegroundColor(context.getResources().getColor(R.color.text_70));
    }

    private static void addMergeText(Context context, SpannableStringUtils.Builder builder, String text, long messageId, int noticeType) {
        builder.append(text).setClickSpan(new MyClickableSpan(context, messageId, true, noticeType));
    }

    private static void addTime(Context context, SpannableStringUtils.Builder builder, long time) {
        builder.append(" " + TimeUtil.getFriendlyTime(time)).setForegroundColor(context.getResources().getColor(R.color.text_bc)).setProportion(0.8f);
    }

    /**
     * 拼装文案, 通知,我的
     *
     * @param context
     * @param model
     * @return
     */
    public static SpannableStringBuilder getTextMine(Context context, NoticeModel model) {
        SpannableStringUtils.Builder builder = SpannableStringUtils.getBuilder("");
        List<NoticeModel.NoticeFeedModel> feedModels = model.getFeeds();
        if (feedModels != null) {
            setUserName(context, model.getUsers(), model.getMergeSize(), model.getId(), builder, NoticeChildFragment.TYPE_MERGE_MINE);
            switch (model.getEventType()) {
                case 1:
                    addNormalText(context, builder, context.getString(R.string.notice_follow_you));
                    break;
                case 2:
                    if (!model.isMerge()) {
                        addNormalText(context, builder, String.format(context.getString(R.string.notice_like), getContentByType(context, feedModels)));
                    } else {
                        if (model.getUsers().size() == 1) {
                            addMergeText(context, builder, String.format(context.getString(R.string.notice_like_merge), feedModels.size(), getContentByType(context, feedModels)), model.getId(), NoticeChildFragment.TYPE_MERGE_MINE);
                        } else {
                            addNormalText(context, builder, String.format(context.getString(R.string.notice_like), getContentByType(context, feedModels)));
                        }
                    }

                    break;
                case 3:
                    addNormalText(context, builder, context.getString(R.string.notice_at_you));
                    addNormalText(context, builder, getFormatComment(model.getComment()));
                    setAtUserName(context, model.getAtUsers(), builder);
                    break;

                case 6:
                    if (model.isMerge()) {
                        String content = String.format(context.getString(R.string.notice_comment), getContentByType(context, feedModels));
                        addNormalText(context, builder, content);
                    } else {
                        addNormalText(context, builder, context.getString(R.string.say_something_to_you));
                        addNormalText(context, builder, getFormatComment(model.getComment()));
                        setAtUserName(context, model.getAtUsers(), builder);
                    }

                    break;
                case 7:
                    if (model.isMerge()) {
                        String content = String.format(context.getString(R.string.notice_forward_merge), getContentByType(context, feedModels));
                        addNormalText(context, builder, content);
                    } else {
                        String content = String.format(context.getString(R.string.notice_forward), getContentByType(context, feedModels));
                        addNormalText(context, builder, content);
                        addNormalText(context, builder, getFormatComment(model.getComment()));
                        setAtUserName(context, model.getAtUsers(), builder);
                    }

                    break;

            }
            addTime(context, builder, model.getCreateTime());

            return builder.create();
        } else {
            return builder.create();
        }
    }

    /**
     * 拼装文案, 通知,我的,合并消息
     *
     * @param context
     * @param model
     * @return
     */
    public static SpannableStringBuilder getTextMineMerge(Context context, NoticeMergeMineModel model) {
        SpannableStringUtils.Builder builder = SpannableStringUtils.getBuilder("");
        NoticeModel.NoticeFeedModel feed = model.getFeed();
        addUserNameSpanable(context, builder, model.getUser(), 0);
        switch (model.getEventType()) {
            case 1:
                addNormalText(context, builder, context.getString(R.string.notice_follow_you));
                break;
            case 2:
                if (feed != null) {
                    addNormalText(context, builder, String.format(context.getString(R.string.notice_like), getContentByType(context, feed)));
                }
                break;
            case 3:
                addNormalText(context, builder, context.getString(R.string.notice_at_you));
                addNormalText(context, builder, getFormatComment(model.getComment()));
                setAtUserName(context, model.getAtUsers(), builder);
                break;
            case 4:
                if (feed != null) {
                    String content = String.format(context.getString(R.string.notice_gift), getContentByType(context, feed));
                    addNormalText(context, builder, content);
//                    addNormalText(context, builder, model.getGiftId());
//                    setAtUserName(context,model,builder);
                }
                break;
            case 6:
                if (feed != null) {
                    String content = String.format(context.getString(R.string.notice_comment), getContentByType(context, feed));
                    addNormalText(context, builder, content);
                    addNormalText(context, builder, getFormatComment(model.getComment()));
                    setAtUserName(context, model.getAtUsers(), builder);
                }
                break;
            case 7:
                if (feed != null) {
                    String content = String.format(context.getString(R.string.notice_forward), getContentByType(context, feed));
                    addNormalText(context, builder, content);
                    addNormalText(context, builder, getFormatComment(model.getComment()));
                    setAtUserName(context, model.getAtUsers(), builder);
                }
                break;

        }
        addTime(context, builder, model.getCreateTime());

        return builder.create();
    }


    private static void setUserName(Context context, List<UserModel> userModels, int mergeSize, int messageId, SpannableStringUtils.Builder builder, int noticeType) {

        if (userModels == null) {
            return;
        }
        switch (userModels.size()) {
            case 1:
                addUserNameSpanable(context, builder, userModels.get(0), noticeType);

                break;
            case 2:
                for (int i = 0; i < 2; i++) {
                    addUserNameSpanable(context, builder, userModels.get(i), noticeType);
                    if (i == 0) {
                        addNormalText(context, builder, context.getString(R.string.notice_mark));
                    } else if (i == 1) {
                        addNormalText(context, builder, context.getString(R.string.notice_and));
                    }
                }
                addMergeText(context, builder, String.format(context.getString(R.string.notice_other_follower), mergeSize), messageId, noticeType);
                break;
            case 3:
                for (int i = 0; i < userModels.size(); i++) {
                    addUserNameSpanable(context, builder, userModels.get(i), noticeType);
                    if (i == 0) {
                        addNormalText(context, builder, context.getString(R.string.notice_mark));
                    } else if (i == 1) {
                        addNormalText(context, builder, context.getString(R.string.notice_and));
                    }
                }
                break;
        }

    }


    private static void setAtUserName(Context context, List<UserModel> atUserList, SpannableStringUtils.Builder builder) {
        if (atUserList != null) {
            for (int i = 0; i < atUserList.size(); i++) {
                addAtUserNameSpanable(context, builder, atUserList.get(i), NoticeChildFragment.TYPE_MERGE_FOLLOW);
            }
        }
    }

    private static String getContentByType(Context context, List<NoticeModel.NoticeFeedModel> noticeFeedModel) {
        if (noticeFeedModel != null) {
            boolean hasImage = false;
            boolean hasVedio = false;
            int size = noticeFeedModel.size();
            for (NoticeModel.NoticeFeedModel model : noticeFeedModel) {
                if (!hasImage && model.isImage()) {
                    hasImage = true;
                }
                if (!hasVedio && !model.isImage()) {
                    hasVedio = true;
                }
            }
            String str;
            if (hasImage && hasVedio) {
                str = context.getString(R.string.notice_dynamic_merge);
            } else if (hasImage) {
                str = size > 1 ? context.getString(R.string.notice_image_merge) : context.getString(R.string.notice_image);
            } else {
                str = size > 1 ? context.getString(R.string.notice_video_merge) : context.getString(R.string.notice_video);

            }
            return str;
        }
        return "";
    }

    /**
     * 通知,我关注的
     *
     * @param context
     * @param model
     * @return
     */
    public static SpannableStringBuilder getTextMineFollow(Context context, NoticeMineFollowModel model) {
        SpannableStringUtils.Builder builder = SpannableStringUtils.getBuilder("");
        List<NoticeModel.NoticeFeedModel> feedModels = model.getFeeds();
        if (model.getOperatorUser() != null) {
            addUserNameSpanable(context, builder, model.getOperatorUser().get(0), NoticeChildFragment.TYPE_MERGE_FOLLOW);
        }
        if (feedModels != null) {
            switch (model.getEventType()) {
                case 1:
                    addNormalText(context, builder, context.getString(R.string.notice_follow_follow));
                    setUserName(context, model.getUsers(), model.getMergeSize(), model.getId(), builder, NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    break;
                case 2:
                    if (model.isMerge()) {
                        addMergeText(context, builder, String.format(context.getString(R.string.notice_follow_like_merge), feedModels.size(), getContentByType(context, feedModels)), model.getId(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    } else {
                        addNormalText(context, builder, context.getString(R.string.notice_follow_like));
                        setUserName(context, model.getUsers(), model.getMergeSize(), model.getId(), builder, NoticeChildFragment.TYPE_MERGE_FOLLOW);
                        addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feedModels.get(0))));
                    }


                    break;

                case 6:
                    if (feedModels != null && feedModels.size() > 0) {
                        addNormalText(context, builder, context.getString(R.string.notice_follow_comment));
                        setUserName(context, model.getUsers(), model.getMergeSize(), model.getId(), builder, NoticeChildFragment.TYPE_MERGE_FOLLOW);
                        addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feedModels)));
                        addNormalText(context, builder, ":" + getFormatComment(model.getComment()));
                        setAtUserName(context, model.getAtUsers(), builder);
//
                    }
                    break;
                case 7:
                    if (feedModels != null && feedModels.size() > 0) {
                        addNormalText(context, builder, context.getString(R.string.notice_follow_forward));
                        setUserName(context, model.getUsers(), model.getMergeSize(), model.getId(), builder, NoticeChildFragment.TYPE_MERGE_FOLLOW);
                        addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feedModels)));
                        addNormalText(context, builder, ":" + getFormatComment(model.getComment()));
                        setAtUserName(context, model.getAtUsers(), builder);
                    }
                    break;
                case 8:
                    if (model.isMerge()) {
                        addMergeText(context, builder, String.format(context.getString(R.string.notice_follow_release_merge), feedModels.size(), getContentByType(context, feedModels)), model.getId(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    } else {
                        addNormalText(context, builder, context.getString(R.string.notice_follow_release));
                        addNormalText(context, builder, getContentByType(context, feedModels));
                    }
                    break;
                case 9: // 超胆播
                    NoticeModel.NoticeShortLiveInfo shortLive = model.getShortLive();
                    if (shortLive != null) {
                        addNormalText(context, builder, context.getString(R.string.notice_forward_super_live_1));
                        addSuperLiveSpanable(context, builder, shortLive, NoticeChildFragment.TYPE_FOLLOW, model.getEventType());
                        addNormalText(context, builder, context.getString(R.string.notice_forward_super_live_2));
                    }
                    break;
            }
            addTime(context, builder, model.getCreateTime());

            return builder.create();
        } else {
            return builder.create();
        }
    }

    /**
     * 通知,我关注的
     *
     * @param context
     * @param model
     * @return
     */
    public static SpannableStringBuilder getTextMineFollowMerge(Context context, NoticeMergeMineFollowModel model) {
        SpannableStringUtils.Builder builder = SpannableStringUtils.getBuilder("");
        NoticeModel.NoticeFeedModel feed = model.getFeed();
        addUserNameSpanable(context, builder, model.getOperatorUser(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
        switch (model.getEventType()) {
            case 1:
                addNormalText(context, builder, context.getString(R.string.notice_follow_follow));
                addUserNameSpanable(context, builder, model.getTargetUser(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                break;
            case 2:
                if (feed != null) {
                    addNormalText(context, builder, context.getString(R.string.notice_follow_like));
                    addUserNameSpanable(context, builder, model.getTargetUser(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feed)));
                }

                break;
            case 5:
                if (feed != null) {
                    addNormalText(context, builder, context.getString(R.string.notice_follow_love));
                    addUserNameSpanable(context, builder, model.getTargetUser(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feed)));
                    addNormalText(context, builder, context.getString(R.string.notice_follow_send));
                }
                break;
            case 6:
                if (feed != null) {
                    addNormalText(context, builder, context.getString(R.string.notice_follow_comment));
                    addUserNameSpanable(context, builder, model.getTargetUser(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feed)));
                    addNormalText(context, builder, ":" + getFormatComment(model.getComment()));
                    setAtUserName(context, model.getAtUsers(), builder);
//
                }
                break;
            case 7:
                if (feed != null) {
                    addNormalText(context, builder, context.getString(R.string.notice_follow_forward));
                    addUserNameSpanable(context, builder, model.getTargetUser(), NoticeChildFragment.TYPE_MERGE_FOLLOW);
                    addNormalText(context, builder, String.format(context.getString(R.string.notice_follow_content), getContentByType(context, feed)));
                    addNormalText(context, builder, ":" + getFormatComment(model.getComment()));
                    setAtUserName(context, model.getAtUsers(), builder);
                }
                break;
        }
        addTime(context, builder, model.getCreateTime());
        return builder.create();
    }

    private static String getFormatComment(String comment) {
        if (StringUtil.isEmpty(comment)) {
            return "";
        }
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(comment);
        comment = m.replaceAll("");
        return comment;
    }

    private static String getContentByType(Context context, NoticeModel.NoticeFeedModel noticeFeedModel) {
        return noticeFeedModel.isImage() ? context.getString(R.string.notice_image) : context.getString(R.string.notice_video);
    }
}
